package cc.rengu.igas.channel.cpcn.core.service.mchntorder;

import cc.rengu.igas.channel.cpcn.common.constant.CpcnParamConstant;
import cc.rengu.igas.channel.cpcn.common.constant.CpcnTreeNodeConstant;
import cc.rengu.igas.channel.cpcn.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cpcn.common.util.CpcnTransferUtil;
import cc.rengu.igas.channel.cpcn.core.service.base.NoticeService;
import cc.rengu.igas.opms.facade.base.Header;
import cc.rengu.igas.opms.facade.request.CpcnOrderRefundNoticeRequest;
import cc.rengu.igas.opms.facade.response.CpcnOrderRefundNoticeResponse;
import cc.rengu.igas.opms.facade.result.Result;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/27 18:22
 */
public class CpcnMchntOrderRefundTransResultNoticeService extends NoticeService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CpcnOrderRefundNoticeRequest cpcnOrderRefundNoticeRequest = new CpcnOrderRefundNoticeRequest();
        ConvertUtil.convertOutput(cpcnOrderRefundNoticeRequest);
        rglog.debug("cpcnOrderRefundNoticeRequest = <{}>", cpcnOrderRefundNoticeRequest);
        return cpcnOrderRefundNoticeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        CpcnOrderRefundNoticeRequest cpcnOrderRefundNoticeRequest = (CpcnOrderRefundNoticeRequest) request;
        cpcnOrderRefundNoticeRequest.setHeader(new Header());
        cpcnOrderRefundNoticeRequest.getHeader().setInstId(CpcnTransferUtil.cpcnInstIdTransfer2CorpBank(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID)));
        cpcnOrderRefundNoticeRequest.getHeader().setChanlId(CpcnParamConstant.CHANNEL_ID_CPCN);
        cpcnOrderRefundNoticeRequest.getHeader().setSrcSysId("OPMS");
        cpcnOrderRefundNoticeRequest.getHeader().setTxnNum("T00PM062");
        cpcnOrderRefundNoticeRequest.getHeader().setBizType(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.BIZ_TYPE));
        cpcnOrderRefundNoticeRequest.setInstitutionID(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.INSTITUTION_ID));
        cpcnOrderRefundNoticeRequest.setRefundStatus(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.STATUS));
        cpcnOrderRefundNoticeRequest.setOrderAmt(AmountUtil.changeF2Y(xmlTreeUtil.getXmlTreeStringValue(CpcnTreeNodeConstant.AMOUNT)));
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnOrderRefundNoticeResponse> responseResult = (Result<CpcnOrderRefundNoticeResponse>) dubboService.callDubboService(CpcnParamConstant.DUBBO_BEAN_OPMS_PAY, CpcnParamConstant.DUBBO_METHOD_CPCN_REFUND_NOTICE, cpcnOrderRefundNoticeRequest);
        if (null == responseResult || !responseResult.isSuccess() || !"00000000".equals(responseResult.getResult().getRespCode())) {
            rglog.error("支付结果通知失败");
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, RespCodeEnum.SYSTEM_ERROR.getRespCode());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        } else {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, RespCodeEnum.SUCCESS.getRespCode());
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, RespCodeEnum.SUCCESS.getRespDesc());
        }
        return null;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }
}
